package com.yj.service.impl;

import com.yj.entity.Collectionss;
import com.yj.entity.Users;
import com.yj.dao.CollectionssMapper;
import com.yj.dao.HousesMapper;
import com.yj.dao.UsersMapper;
import com.yj.service.ICollectionssService;
import com.yj.vo.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 * 收藏表
 * 
 * @author yj
 * @since 2020-08-24
 */
@Service
public class CollectionssServiceImpl extends ServiceImpl<CollectionssMapper, Collectionss>
		implements ICollectionssService {

	@Autowired
	CollectionssMapper coll;
	@Autowired
	UsersMapper user;
	@Autowired
	HousesMapper house;

	@Override
	public JsonResult showCollection(int uid) {
		JsonResult ret = null;
		List<Map<String, Object>> infoList = new ArrayList<Map<String, Object>>();

		QueryWrapper<Collectionss> wrapper = new QueryWrapper<Collectionss>();
		wrapper.eq("uid", uid);
		String uname = user.selectById(uid).getUname();
		List<Collectionss> list = coll.selectList(wrapper);
		if (list != null) {
			ret = new JsonResult(true, "收藏查找成功");
			for (Collectionss co : list) {
				String hinfo = house.selectById(co.getHid()).getHinfo();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uname", uname);
				map.put("houseinfo", hinfo);
				map.put("collection", co);
				infoList.add(map);
			}
			ret.setData("info", infoList);
			return ret;
		} else {
			ret = new JsonResult(false, "收藏查询失败");
			return ret;
		}
	}

	@Override
	public JsonResult deleteCollection(int cid) {
		JsonResult ret = null;
		int n = coll.deleteById(cid);
		if (n != 0) {
			ret = new JsonResult(true, "删除成功");
			ret.setData("info", "删除成功");
			return ret;
		} else {
			ret = new JsonResult(false, "删除失败");
			return ret;
		}
	}

	@Override
	public JsonResult addCollection(Collectionss collectionss) {
		JsonResult ret = null;
		int insert = coll.insert(collectionss);
		if (insert != 0) {
			ret = new JsonResult(true, "收藏成功");
			ret.setData("info", "收藏成功");
			return ret;
		} else {
			ret = new JsonResult(false, "收藏失败");
			return ret;
		}
	}

}
